package com.css.dzj.zfjg.modules.supervisioncompinfo.controller;


import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.JgFbComplaintEntity;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.JgTsjbComplaintInfoEntity;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.JgTsjbUrgeInfoEntity;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgBlComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgFbComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.vo.JgTsMsg;
import com.css.dzj.zfjg.modules.supervisioncompinfo.vo.TsXqVo;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 监管投诉信息办理controller
 * @author xgdou
 * @date 2020/05/25
 */
@RestController
@RequestMapping(value = "/lhjgtsjgbl/")
public class JgBlComplaintController {

    @Autowired
    JgBlComplaintService jgBlComplaintService;
    @Autowired
    JgFbComplaintService jgFbComplaintService;
    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 根据查询条件查询展示
     *
     * @param pageSize
     * @param pageNo
     * @param complaintTerritory
     * @param complaintObject
     * @param complaintType
     * @param name
     * @param taskState
     * @param fbStartTime
     * @param fbEndTime
     */
    @GetMapping("listFbComplaininfoEntity")
    public R listJgFbEntity(@RequestParam Integer pageSize,
                            @RequestParam Integer pageNo,
                            @RequestParam String complaintTerritory,
                            @RequestParam String complaintObject,
                            @RequestParam String complaintType,
                            @RequestParam String name,
                            @RequestParam String taskState,
                            @RequestParam(required = false) String fbStartTime,
                            @RequestParam(required = false) String fbEndTime
    ) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgTsMsg> JgFbComplaintEntityQueryResults = jgBlComplaintService.getComplainEntity(pageRequest,complaintTerritory,complaintObject, complaintType, name, taskState,fbStartTime,fbEndTime);
            s = JSON.toJSONString(JgFbComplaintEntityQueryResults);
            sysAuditService.saveSuccessAudit("多条件分页查询监管投诉信息成功", Constant.ZFJG_LHJGTSJGBL, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", JgFbComplaintEntityQueryResults);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("多条件分页查询监管投诉信息失败", Constant.ZFJG_LHJGTSJGBL, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 获取任务查收页面
     * @param registerCode
     * @return 监管投诉信息表  任务分办表  任务催办表
     */
    @GetMapping("getcsinfo")
    public R getfbinfo(@RequestParam String registerCode) {
        String s = "";
        try{
            TsXqVo tsXqVo3 = new TsXqVo();
            //监管投诉信息
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity = jgFbComplaintService.findNo(registerCode);
            //监管任务分办表
            JgFbComplaintEntity jgFbComplaintEntity = jgFbComplaintService.findRegister(registerCode);
            //任务催办表
            JgTsjbUrgeInfoEntity jgTsjbUrgeInfoEntity = jgFbComplaintService.findCode(registerCode);
            tsXqVo3.setJgTsjbComplaintInfoEntity(jgTsjbComplaintInfoEntity);
            tsXqVo3.setJgFbComplaintEntity(jgFbComplaintEntity);
            tsXqVo3.setJgTsjbUrgeInfoEntity(jgTsjbUrgeInfoEntity);
            s = JSON.toJSONString(tsXqVo3);
            sysAuditService.saveSuccessAudit("查询详情成功", Constant.ZFJG_LHJGTSJGBL, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("tsXqVo3", tsXqVo3);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询详情失败", Constant.ZFJG_LHJGTSJGBL, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 任务查收保存flag 102查收  104拒绝
     */
    @PostMapping("save/{flag}")
    public R save(@RequestBody TsXqVo tsXqVo, HttpServletRequest request,@PathVariable String flag) {
        String s = "";
        try{
            //将分办信息入库
            JgFbComplaintEntity jgFbComplaintEntity = tsXqVo.getJgFbComplaintEntity();
            JgFbComplaintEntity oldJgTaskinfoEntity = jgFbComplaintService.findRegister(jgFbComplaintEntity.getRegisterCode());
            if(oldJgTaskinfoEntity!=null){
                BeanUtil.copyBeanNotEmptyToBean(jgFbComplaintEntity, oldJgTaskinfoEntity);
                jgBlComplaintService.update(jgFbComplaintEntity,request,flag);
            }else{
                jgFbComplaintService.add(jgFbComplaintEntity, request);
            }
            //将监管投诉信息状态改为已处理 flag102查收  flag104拒绝
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity = tsXqVo.getJgTsjbComplaintInfoEntity();
            jgBlComplaintService.updatetsjb(jgTsjbComplaintInfoEntity,request,flag);
            s = JSON.toJSONString(jgTsjbComplaintInfoEntity);
            sysAuditService.saveSuccessAudit("任务查收成功", Constant.ZFJG_LHJGTSJGBL, Constant.PT, Constant.ZFJG_RECEIVE, s, request);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("任务查收失败", Constant.ZFJG_LHJGTSJGBL, Constant.YZ, Constant.ZFJG_RECEIVE, s, e, request);
            return R.error("提交失败");
        }
    }

}
